package com.sportizz.sport;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class custom_follow_dialog extends Dialog {

	Activity act;
	public Button follow1, follow2;

	boolean press1, press2;

	public TextView player1_txt, player2_txt;

	private DatabaseAdapter mDbHelper;

	public ProgressDialog pd;
	
	public String device_id, player1_id, player1_name, player1_country_cd,
			player2_id, player2_name, player2_country_cd, sport_cd, sport,
			isteam, country_cd;
	
	
	public custom_follow_dialog(Activity context, String device_id,
			String player1_id, String player1_name, String player1_country_cd,
			String player2_id, String player2_name, String player2_country_cd,
			String sport_cd, String sport, String isteam) {

		super(context);
		this.act = context;
		this.device_id = device_id;
		this.player1_id = player1_id;
		this.player1_name = player1_name;
		this.player1_country_cd = player1_country_cd;
		this.player2_id = player2_id;
		this.player2_name = player2_name;
		this.player2_country_cd = player2_country_cd;
		this.sport_cd = sport_cd;
		this.sport = sport;
		this.isteam = isteam;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbHelper = new DatabaseAdapter(act);

		country_cd = StartActivity.GetCountryCode(act);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.custom_follow_double);

		player1_txt = (TextView) findViewById(R.id.follow_txtname1);

		player2_txt = (TextView) findViewById(R.id.follow_txtname2);

		follow1 = (Button) findViewById(R.id.follow_double1);
		follow2 = (Button) findViewById(R.id.follow_double2);

		Typeface font_player = Typeface.createFromAsset(act.getAssets(),
				"hel57.ttf");
		player1_txt.setTypeface(font_player);
		player2_txt.setTypeface(font_player);

		player1_txt.setText("");
		player2_txt.setText("");

		if (player1_name.equalsIgnoreCase("NONE")
				|| player1_name.equalsIgnoreCase("null")) {
			player1_txt.setText("");
		} else {
			player1_txt.setText(player1_name);
			player1_txt.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
			player1_txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		}

		if (player2_name.equalsIgnoreCase("NONE")
				|| player2_name.equalsIgnoreCase("null")) {
			player2_txt.setText("");
		} else {
			player2_txt.setText(player2_name);
			player2_txt.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
			player2_txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
		}

		follow1.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mDbHelper.open();

				StartActivity.foll_count = StartActivity.follow_count.getInt(
						"follow_count", 0);

				Log.e("suraj", "StartActivity.foll_count = "
						+ StartActivity.foll_count);

//				if (StartActivity.foll_count < StartActivity.limited_follow
//						.getInt("max_follow", 3)
//						&& StartActivity.foll_count >= 0) {
					String btn_name = follow1.getText().toString();

					if (btn_name.equals("follow")) {

						AsyncTask<Void, Void, Void> wait = new AsyncTask<Void, Void, Void>() {

							@Override
							protected void onPostExecute(
									Void result) {
								super.onPostExecute(result);
								pd.dismiss();
							
							}

							@Override
							protected void onPreExecute() {
								
								pd = ProgressDialog.show(
										act, "",
										"Following...", true);
								super.onPreExecute();
							}

							@Override
							protected Void doInBackground(
									Void... params) {
						
						Log.d("suraj", "Player = " + device_id + ""
								+ player1_id + "" + player1_name + ""
								+ sport_cd + "" + player1_country_cd + ""
								+ isteam);
						
						
						long c = mDbHelper.insert_follow_info(player1_id, player1_name, sport_cd, sport,
								player1_country_cd, isteam,"P");
						

						String response = Utils
								.getJSONString("http://sportizza.com/interface/ios_follow.php?dev_id="
										+ device_id
										+ "&iso_cd="
										+ country_cd
										+ "&follow="
										+ player1_id
										+ "&follow_cs=-1");

						String Substring = response.substring(0, 4);

						if (Substring != null || c != 0) {
							Thread t = new Thread(
									new Runnable() {
										public void run() {

											act.runOnUiThread(new Runnable() {
												public void run() {
													
							Toast.makeText(act.getApplicationContext(),
									"Added to I follow", Toast.LENGTH_SHORT)
									.show();
							Log.d("suraj", "follow success..");
							follow1.setText("UNFOLLOW");

							MainActivity.playerAll.add(player1_id);

							StartActivity.foll_count++;

							SharedPreferences.Editor editor = StartActivity.follow_count
									.edit();
							editor.putInt("follow_count",
									StartActivity.foll_count);
							editor.commit();
							mDbHelper.close();
												}
											});
										}
									});
							t.start();
						} else {
							Toast.makeText(act.getApplicationContext(),
									"Error during follow", Toast.LENGTH_SHORT)
									.show();
						}

						press1 = true;

						return null;
							}
						};
						wait.execute();
					} 
			}
		});

		follow2.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mDbHelper.open();

				StartActivity.foll_count = StartActivity.follow_count.getInt(
						"follow_count", 0);

				Log.e("suraj", "StartActivity.foll_count = "
						+ StartActivity.foll_count);

					String btn_name = follow2.getText().toString();

					if (btn_name.equals("FOLLOW")) {

						AsyncTask<Void, Void, Void> wait = new AsyncTask<Void, Void, Void>() {

							@Override
							protected void onPostExecute(
									Void result) {
								super.onPostExecute(result);
								pd.dismiss();
							
							}

							@Override
							protected void onPreExecute() {
								
								pd = ProgressDialog.show(
										act, "",
										"Following...", true);
								super.onPreExecute();
							}

							@Override
							protected Void doInBackground(
									Void... params) {
								
						long c = mDbHelper.insert_follow_info(player2_id, player2_name, sport_cd, sport,
								player2_country_cd, isteam,"P");

						String response = Utils
								.getJSONString("http://sportizza.com/interface/ios_follow.php?dev_id="
										+ device_id
										+ "&iso_cd="
										+ country_cd
										+ "&follow="
										+ player1_id
										+ "&follow_cs=-1");

						String Substring = response.substring(0, 4);

						if (Substring != null || c != 0) {
							
							Thread t = new Thread(
									new Runnable() {
										public void run() {

											act.runOnUiThread(new Runnable() {
												public void run() {
													
							Toast.makeText(act.getApplicationContext(),
									"Added to I follow", Toast.LENGTH_SHORT)
									.show();
							Log.d("suraj", "follow success..");
							follow2.setText("UNFOLLOW");

							MainActivity.playerAll.add(player2_id);

							StartActivity.foll_count++;

							SharedPreferences.Editor editor = StartActivity.follow_count
									.edit();
							editor.putInt("follow_count",
									StartActivity.foll_count);
							editor.commit();

							mDbHelper.close();
							
												}
											});
										}
										});
							t.start();
						} else {
							Toast.makeText(act.getApplicationContext(),
									"Error during follow", Toast.LENGTH_SHORT)
									.show();
						}

						press2 = true;

						return null;
							}
						};
						wait.execute();
						
					} 

			}
		});

		Log.d("suraj", "playerAll = " + MainActivity.playerAll);

		Log.d("suraj",
				"follow_btn1.getTag() = "
						+ MainActivity.playerAll.contains(player1_id) + "=="
						+ player1_id);
		Log.d("suraj",
				"follow_btn2.getTag() = "
						+ MainActivity.playerAll.contains(player2_id) + "=="
						+ player2_id);

		if (MainActivity.playerAll != null) {
			if (MainActivity.playerAll.contains(player1_id.toString()) == true) {
				follow1.setText("UNFOLLOW");
			} else {
				follow1.setText("FOLLOW");
			}

			if (MainActivity.playerAll.contains(player2_id.toString()) == true) {
				follow2.setText("UNFOLLOW");
			} else {
				follow2.setText("FOLLOW");
			}
		}
	}
}
